import request, { all } from '@/utils/request'

/**
 * 初始数据
 * @param callback
 * @param perPage
 */
export function initial(callback, params) {
  const url = [
    request.get('/user/paginate', { params: params }),
    request.get('/group/paginate?per_page=1000')
  ]
  all(url, function(res) {
    if (res !== false) {
      callback(res)
    }
  })
}

/**
 * 数据列表
 * @param params
 * @returns AxiosPromise
 */
export function paginate(params) {
  return request({
    url: '/user/paginate',
    method: 'get',
    params: params
  })
}

/**
 * 用户信息
 * @returns {*}
 */
export function info(id = null) {
  const url = id ? '/user/info/' + id : '/user/info'
  return request({ url: url, method: 'get' })
}

/**
 * 保存数据
 * @param params
 * @param id
 * @returns AxiosPromise
 */
export function save(params, id = null) {
  return request({
    url: '/user/' + (id ? 'update/' + id : 'store'),
    method: 'post',
    data: params
  })
}

/**
 * 批量修改数据
 * @param params
 * @param id
 * @returns AxiosPromise
 */
export function clearBond(params) {
  return request({
    url: '/user/clear/bond',
    method: 'post',
    data: params
  })
}

/**
 * 批量修改数据
 * @param params
 * @param id
 * @returns AxiosPromise
 */
export function updateAll(params) {
  return request({
    url: '/user/update/all',
    method: 'post',
    data: params
  })
}

/**
 * 删除数据
 * @param id
 * @returns AxiosPromise
 */
export function destroy(id) {
  return request({
    url: '/user/destroy/' + id,
    method: 'delete'
  })
}

/**
 * 用户登录
 * @param params
 * @returns {*}
 */
export function login(params) {
  return request({
    url: '/login',
    method: 'post',
    data: params,
    prefix: false
  })
}

/**
 * 退出登录
 * @returns {*}
 */
export function logout() {
  return request({
    url: '/logout',
    method: 'get',
    prefix: false
  })
}
